﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class panel_Country : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        
        public panel_Country()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            LoadData();

            this.Country.DoubleClick += Country_DoubleClick;
            this.Country.PopupMenuShowing += Country_PopupMenuShowing;
            this.countryContext.ItemClicked += countryContext_ItemClicked;
        }

        private void callEditForm()
        {
            DataRow row = Country.GetFocusedDataRow();
            frm_Edit_Country editForm = new frm_Edit_Country();
            editForm.country_id = row[0].ToString();
            if (editForm.LoadData())
            {
                //editForm.Show();
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot load data for selected record");
            }
        }

        void Country_DoubleClick(object sender, EventArgs e)
        {
            callEditForm();
        }

        void countryContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.countryContext.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();                    
                    break;
                case "addSelection":
                    frm_Add_Country add_Form = new frm_Add_Country();
                    //add_Form.Show();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "deleteSelection":
                    deleteDatabase();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void Country_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.countryContext.Show(MousePosition);
        }

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT * FROM [222_Country]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridCountry.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Country");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("CountryCode", typeof(int));
                newTable.Columns.Add("CountryName", typeof(string));                
                gridCountry.DataSource = newTable;
            }
            Country.Columns[0].Caption = "Country Code";
            Country.Columns[1].Caption = "Country Name";            
            Country.BestFitColumns();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_Country add_Form = new frm_Add_Country();
            //add_Form.Show();
            add_Form.ShowDialog();
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm();
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteDatabase();
        }

        private void deleteDatabase()
        {
            int[] rows = Country.GetSelectedRows();
            if (rows.Length <= 0)
            {
                MessageBox.Show("There is no selected record to delete");
            }
            else
            {
                if (MessageBox.Show("Are you really to delete selected records now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string[] id_list = new string[rows.Length];
                    for (int i = 0; i < rows.Length; i++)
                    {
                        DataRow selected_row = Country.GetDataRow(rows[i]);
                        id_list[i] = "'" + selected_row[0].ToString() + "'";
                    }
                    string id = string.Join(",", id_list);

                    try
                    {
                        myConnection.Open();
                        string sql = "DELETE [222_Country] WHERE CountryCode IN (" + id + ")";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("Country deleted success.");
                        LoadData();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Cannot delete Country. Please try again.");
                    }
                }
            }            
        }
    }
}
